package com.invite.me.wlqq;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Multipart;
import retrofit2.http.Part;

public class RxJavaDataImpl<T> {
    public static <T> void getData(String url, Observer observer) {
        RetrofitManager.getManagerInstance().getApiService().
                getData(url).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }

    public static <T> void getData(String url, HashMap<String, Object> params, Observer observer) {
        if (params != null) {
            RetrofitManager.getManagerInstance().getApiService().
                    getData(url, params).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribeOn(Schedulers.io()).
                    subscribe(observer);
        } else {
            getData(url, observer);
        }
    }

    public static <T> void getDataa(String url, HashMap<String, Object> params, Observer observer) {
        if (params != null) {
            RetrofitManager.getManagerInstance().getApiService().
                    getDataa(url, params).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribeOn(Schedulers.io()).
                    subscribe(observer);
        } else {
            getData(url, observer);
        }
    }

    public static <T> void getData(String url, Map<String, Object> headerMap, Observer observer) {
        RetrofitManager.getManagerInstance().getApiService().
                getData(url, headerMap).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }
    public static <T> void getData(String url, Map<String, Object> headerMap, RequestBody requestBody, Observer observer) {
        RetrofitManager.getManagerInstance().getApiService().
                getData(url, headerMap).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }



    public static <T> void getData(String url, Map<String, Object> headerMap, HashMap<String, Object> params, Observer observer) {
        RetrofitManager.getManagerInstance().getApiService().
                getData(url, headerMap, params).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }

    public static <T> void putData(String url, Map<String, Object> headersMap,Observer observer) {
        RetrofitManager.getManagerInstance().getApiService().
                putData(url, headersMap).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
    }
    public static <T> void putData(String url, Map<String, Object> headersMap, RequestBody body,Observer observer) {
        RetrofitManager.getManagerInstance().getApiService().
                putData(url, headersMap,body).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
    }

    public static void postData(String url, Map<String, Object> params, Observer observer) {
        if (params != null) {
            RetrofitManager.getManagerInstance().getApiService().
                    postData(url, params).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(observer);
        } else {
            RetrofitManager.getManagerInstance().getApiService().
                    postData(url).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(observer);
        }
    }
    public static void postDataa(String url, Map<String, Object> params, Observer observer) {
        if (params != null) {
            RetrofitManager.getManagerInstance().getApiService().
                    postDataa(url, params).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(observer);
        } else {
            RetrofitManager.getManagerInstance().getApiService().
                    postData(url).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(observer);
        }
    }

    public static <T> void postData(String url, HashMap<String, Object> params, Observer observer) {
        if (params != null) {
            RetrofitManager.getManagerInstance().getApiService().
                    postData(url, params).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribeOn(Schedulers.io()).
                    subscribe(observer);
        } else {
            RetrofitManager.getManagerInstance().getApiService().
                    postData(url).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(observer);
        }
    }

    public static void postData(String url, Map<String, Object> headerMap, Map<String, Object> params, Observer observer) {
        if (params != null) {
            RetrofitManager.getManagerInstance().getApiService().
                    postData(headerMap, url, params).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(observer);
        } else {
            RetrofitManager.getManagerInstance().getApiService().
                    postData(headerMap, url).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(observer);
        }
    }
    public static <T> void postData(String url, Map<String, Object> headersMap, RequestBody body, Observer observer) {
        RetrofitManager.getManagerInstance().getApiService().
                postData(url, headersMap, body).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
    }

//    public static void postDatas(String url, Map<String, Object> headerMap, RequestBody requestBody, Observer observer) {
//
//        RetrofitManager.getManagerInstance().getApiService().
//                postDatas(headerMap,url,requestBody).
//                subscribeOn(Schedulers.io()).
//                observeOn(AndroidSchedulers.mainThread()).
//                subscribe(observer);
//
//    }

    public static <T> void downloadFile(String url, Observer observer) {
        RetrofitManager.getManagerInstance().getApiService().
                downloadFile(url).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }


    public static <T> void uploadFile(String url, Part part, Observer observer) {
        RetrofitManager.getManagerInstance().
                getApiService().uploadFile(url, part).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }

    public static <T> void uploadFile(String url, @Part MultipartBody.Part parts, Observer observer) {
        RetrofitManager.getManagerInstance().
                getApiService().uploadFile(url, parts).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }

    public static <T> void uploadFile(String url, List<Multipart> uploadFiles, Observer observer) {
        RetrofitManager.getManagerInstance().
                getApiService().uploadFiles(url, uploadFiles).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }

    public static <T> void uploadFile(String url, Map<String, Object> headerMap, @Part MultipartBody.Part parts, Observer observer) {
        RetrofitManager.getManagerInstance().
                getApiService().uploadFile(url, headerMap, parts).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }
    public static <T> void uploadFiles(String url, Map<String, Object> headerMap, @Part MultipartBody.Part parts, @Part MultipartBody.Part partsname, Observer observer) {
        RetrofitManager.getManagerInstance().
                getApiService().uploadFiles(url, headerMap,partsname,parts).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }
    public static <T> void uploadFiles(String url, Map<String, Object> headerMap, @Part MultipartBody.Part parts, Observer observer) {
        RetrofitManager.getManagerInstance().
                getApiService().uploadFiles(url, headerMap,parts).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }




    public static <T> void uploadFile(String url, Map<String, Object> headerMap, MultipartBody.Part  APP_KEY, MultipartBody.Part parts, Observer observer) {
        RetrofitManager.getManagerInstance().
                getApiService().uploadFile(url, headerMap,APP_KEY, parts).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(observer);
    }

    public static <T> void postHeader(String url, HashMap<String, String> header, String path, Observer observer) {
        if (header == null) {
            header = new HashMap<>();
        }
//通过一个图片路径，转换类型
        File file = new File(path);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/octet-stream"), file);
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        builder.addFormDataPart("image", file.getName(), requestBody);
//调用方法
        RetrofitManager.getManagerInstance().getApiService().postHeader(url, header, builder.build())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
        // return OkHttpUtil.getInterface();
    }




    public static RequestBody getRequestBody(String str) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json,charset-UTF-8"), str);
        return requestBody;
    }
}
